package com.gmlive.common.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gmlive.common.ui.R;
import com.gmlive.common.ui.util.KeybordHelper;
import com.gmlive.common.ui.widget.PasswordEditText;

/**
 * @Author: 姜志敏
 * @CreateDate: 2019-07-10 21:30
 */
public class IkPasswordInputDialog extends IkAlertDialog {

    public IkPasswordInputDialog(Context context) {
        this(context, 0);
    }

    public IkPasswordInputDialog(Context context, int themeResId) {
        super(context, themeResId);

        init(context);
    }

    protected IkPasswordInputDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        this(context, 0);
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
    }

    @Override
    public void show() {
        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                if (mPasswordInputEdit != null) {
                    KeybordHelper.showSoftInput(mPasswordInputEdit);
                }
            }
        });
        super.show();
    }

    private TextView mTitleView;
    private PasswordEditText mPasswordInputEdit;
    private TextView mHintView;
    private Button mButton;
    private TextView mTipsView;

    private int mThemeColor;

    private int mHintNormalColor = 0xff999999;

    private PasswordEditText.IOnInputListener mInputListener = new PasswordEditText.IOnInputListener() {
        @Override
        public void onInput(boolean filled, String text) {
            if (mButton != null) {
                mButton.setEnabled(filled);
            }
        }
    };

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.ik_password_dialog, null);
        setView(view);

        mTitleView = view.findViewById(R.id.title);
        mPasswordInputEdit = view.findViewById(R.id.passwordEdit);
        mHintView = view.findViewById(R.id.inputHint);
        mButton = view.findViewById(R.id.action_button);
        mTipsView = view.findViewById(R.id.tv_input_tips);

        mThemeColor = mAlert.getThemeColor();

        mPasswordInputEdit.setListener(mInputListener);
        mButton.setEnabled(false);
    }

    public void setTitle(CharSequence title) {
        mTitleView.setText(title);
    }

    public void setPasswordLength(int length) {
        mPasswordInputEdit.setPasswordLength(length);
    }

    /**
     * 设置密码输入提示文案
     *
     * @param hint        提示内容
     * @param showAsError 是否错误
     */
    public void setHintText(CharSequence hint, boolean showAsError) {
        if (hint != null) {
            mHintView.setVisibility(View.VISIBLE);
            if (showAsError) {
                mHintView.setTextColor(mThemeColor);
            } else {
                mHintView.setTextColor(mHintNormalColor);
            }

            mHintView.setText(hint);
        } else {
            mHintView.setVisibility(View.GONE);
        }
    }

    public void setActionButton(CharSequence text, final ActionClickListener listener) {
        mButton.setText(text);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击不关闭对话框
                listener.onClick(IkPasswordInputDialog.this, mPasswordInputEdit.getPasswordText());
            }
        });
    }

    public void setTips(CharSequence tips) {
        if (tips != null) {
            mTipsView.setText(tips);
            mTipsView.setVisibility(View.VISIBLE);
        } else {
            mTipsView.setVisibility(View.GONE);
        }
    }

    public void setTips(CharSequence tips, int color, View.OnClickListener clickListener) {
        if (tips != null) {
            mTipsView.setTextColor(color);
            mTipsView.setText(tips);
            mTipsView.setVisibility(View.VISIBLE);

            mTipsView.setOnClickListener(clickListener);
        } else {
            mTipsView.setVisibility(View.GONE);
        }
    }

    public interface ActionClickListener {
        void onClick(DialogInterface dialog, String password);
    }
}
